﻿using System;
using System.Collections.Generic;
using System.Text;

namespace X3DLib
{
    public class X3dArea//:X3dInterfaceObject
    {

        private int number; //数组的大小

        public int Number
        {
            get { return number; }
            set { number = value; }
        }
        private string profile;//区域的形状：方（square）或圆（round）

        public string Profile
        {
            get { return profile; }
            set { profile = value; }
        }
        private double[] origin; //原点 

        public double[] Origin
        {
            get { return origin; }
            set { origin = value; }
        }
        private double radius; //半径

        public double Radius
        {
            get { return radius; }
            set { radius = value; }
        }
        private string control;//生成方式：随机（random）或整齐（order）

        public string Control
        {
            get { return control; }
            set { control = value; }
        }
        private string[] translationArr;//设定位置
        /// <summary>
        /// 构造函数
        /// </summary>
        public X3dArea()
        {
            this.number = 10;
            this.profile = "square";
            this.origin = new double[] { 0.0f, 0.0f, 0.0f };
            this.radius = 5.0f;
            this.control = "random";
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="profile">区域形状：方（square）或圆（round）或半圆（halfround）或月牙（moon）</param>
        /// <param name="control">生成方式：随机（random）或整齐（order）</param>
        /// <param name="origin">原点</param>
        /// <param name="radius">半径</param>
        /// <param name="number">数量</param>
        public X3dArea(string profile, string control, double[] origin, double radius, int number)
        {
            this.number = number;
            this.profile = profile;
            this.origin = origin;//数组可以赋值吗？
            this.radius = radius;
            this.control = control;
        }

        public string[] createTranslationArr()
        {
            translationArr = new string[this.number];
            #region 方形区域
            if (this.profile.Equals("square"))//square=方形
            {
                if (this.control.Equals("random"))//如果random,则随机生成
                {
                    //可以使用两种方式初始化一个随机数发生器： 
                    //第一种方法不指定随机种子，系统自动选取当前时间作为随机种子： 
                    //Random ro = new Random(); 
                    //第二种方法可以指定一个int型参数作为随机种子： 
                    //int iSeed=10; 
                    //Random ro = new Random(10);
                    //这样可以保证99%不是一样。
                    long tick = DateTime.Now.Ticks;
                    Random rand = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
                    double dtx,dtz; 
                    for (int i = 0; i < this.number; i++)
                    {
                        //double dsx = Math.random() * (1 - 0.1) + 0.1;//比例0.1--1
                        //double dsy = Math.random() * (1 - 0.1) + 0.1;
                        //double dsz = Math.random() * (1 - 0.1) + 0.1;
                        dtx= rand.NextDouble() * (this.radius - 0.1) + 0.1;//Math.random() * (width - 0.1) + 0.1;
                        dtz = rand.NextDouble() * (this.radius - 0.1) + 0.1; ;//Math.random() * (width - 0.1) + 0.1;
                        //copystr = copystr + "<Transform scale='" + dsx + " " + dsy + " " + dsz + "' translation='" + dtx + " 0 " + dtz + "'>" +
                        //        "\n        <Inline url='treebuild.x3d' />\n" +
                        //        "</Transform>\n";
                        translationArr[i] = dtx.ToString() + " " + "0 " + dtz.ToString();
                    }
                }//end of random
                else
                {
                    int rownum = (int)this.radius / 2;//列数
                    //System.Windows.Forms.MessageBox.Show(rownum.ToString());
                    if (rownum < 1)
                        rownum = 1;
                    if (rownum == 1)
                    {
                        //double dsxyz = 1.0;
                        double dtz = this.radius;
                        double coldst = this.radius / this.number;//行距
                        for (int i = 0; i < this.number; i++)
                        {
                            //copystr = copystr + "<Transform scale='" + dsxyz + " " + dsxyz + " " + dsxyz + "' translation='" + width + " 0 " + dtz + "'>" +
                            //        "\n        <Inline url='treebuild.x3d' />\n" +
                            //        "</Transform>\n";
                            //dsxyz = dsxyz - 0.05;
                            dtz = dtz - coldst;
                            translationArr[i] = this.radius.ToString() + " " + "0 " + dtz.ToString();
                        }
                    }
                    else if (rownum > 1)
                    {
                        //double dsxyz = 1.0;
                        int col = this.number/rownum;//行数
                        double coldst = this.radius / col;//行距
                        double dtz = this.radius;
                        double copyofradius = this.radius;
                        for (int j = 0; j < number; j++)
                        {
                                dtz = dtz - coldst;
                                if (j % col == 0)
                                {
                                    copyofradius = copyofradius + coldst;//树木列间距为2，暂定
                                    dtz = this.radius;
                                }
                                translationArr[j] = copyofradius.ToString() + " " + "0 " + dtz.ToString();                                                       
                        }
                        //this.radius = copyofradius;//将半径的值还原
                    }
                }//end of line
            }//end of sequare
            #endregion
            #region 圆形区域
            else if (this.profile.Equals("round"))//圆（round）
            {
                //if (this.control.Equals("random"))//如果random,则随机生成
                {
                    //可以使用两种方式初始化一个随机数发生器： 
                    //第一种方法不指定随机种子，系统自动选取当前时间作为随机种子： 
                    //Random ro = new Random(); 
                    //第二种方法可以指定一个int型参数作为随机种子： 
                    //int iSeed=10; 
                    //Random ro = new Random(10);
                    //这样可以保证99%不是一样。
                    long tick = DateTime.Now.Ticks;
                    Random rand = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
                    //先获得一半的半圆，然后另一半取负
                    int halfnumber = (this.number)/2;
                    double dtx, dtz;
                    for (int i = 0; i < halfnumber ; i++)
                    {                        
                        //int count = 0;
                        do
                        {
                            //count++;
                            dtx = rand.NextDouble() * (this.radius - 0.1) + 0.1;//Math.random() * (width - 0.1) + 0.1;
                            dtz = rand.NextDouble() * (this.radius - 0.1) + 0.1; ;//Math.random() * (width - 0.1) + 0.1;
                        } while ((dtx * dtx + dtz * dtz) >= this.radius * this.radius); //while ((dtx * dtx + dtz * dtz) <= this.radius * this.radius) ;
                        /////////////////////////////////////////////////////////////////
                        // 存在问题：没有设置原点，所以圆的方程错误，应该是（(dtx-orgin[0]) *(dtx-orgin[0]) + (dtz-orgin[2]) * (dtz-orgin[2])) <= this.radius * this.radius）
                        /////////////////////////////////////////////////////////////////
                        //System.Windows.Forms.MessageBox.Show(count.ToString());
                        //网上的做法：
                        //产生随机浮点数，在正负范围内
                        //Random rd = new Random();
                        //Double d = rd.NextDouble();
                        //Int32 i = rd.Next(0, 2);
                        //Int32 sign = (i == 0 ? 1 : -1);
                        //Double res = 5 * d * sign;
                        //Console.WriteLine(res);

                        if (i % 2 == 0)
                        {
                            //dtx = -dtx;
                            dtz = -dtz;
                        }
                        translationArr[i] = dtx.ToString() + " " + "0 " + dtz.ToString();
                        translationArr[i + halfnumber] = "-"+translationArr[i];
                    }
                    if (this.number % 2 != 0)
                    {
                        dtx = rand.NextDouble() * (this.radius - 0.1) + 0.1;
                        dtz = rand.NextDouble() * (this.radius - 0.1) + 0.1;
                        translationArr[this.number - 1] = dtx.ToString() + " " + "0 " + dtz.ToString();
                    }
                }//end of random
                //else
                {
                    //int rownum = (int)this.radius / 2;//列数
                    //if (rownum < 1)
                    //    rownum = 1;
                    //if (rownum == 1)
                    //{
                    //    //double dsxyz = 1.0;
                    //    int dtz = 5;
                    //    for (int i = 0; i < this.number; i++)
                    //    {
                    //        dtz = dtz - 2;
                    //        translationArr[i] = this.radius.ToString() + " " + "0 " + dtz.ToString();
                    //    }
                    //}
                    //else if (rownum > 1)
                    //{
                    //    //double dsxyz = 1.0;
                    //    int dtz = 5;
                    //    double copyofradius = this.radius;
                    //    for (int j = 0; j < rownum; j++)
                    //    {
                    //        for (int i = 0; i < this.number; i++)
                    //        {
                    //            dtz = dtz - 2;
                    //            translationArr[i] = copyofradius.ToString() + " " + "0 " + dtz.ToString();
                    //        }
                    //        //dtz = 5;
                    //        copyofradius = copyofradius + 2;//树木列间距为2，暂定
                    //    }
                    //    //this.radius = copyofradius;//将半径的值还原
                    //}
                }//end of line
            }
            #endregion
            #region 半圆区域
            else if(this.profile.Equals("halfround"))//半圆（halfround）
            {
                //if (this.control.Equals("random"))//如果random,则随机生成
                {
                    long tick = DateTime.Now.Ticks;
                    Random rand = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
                    double dtx, dtz;

                    for (int i = 0; i < this.number; i++)
                    {                        
                        do
                        {
                            dtx = rand.NextDouble() * (this.radius - 0.1) + 0.1;//Math.random() * (width - 0.1) + 0.1;
                            dtz = rand.NextDouble() * (this.radius - 0.1) + 0.1; ;//Math.random() * (width - 0.1) + 0.1;
                        } while ((dtx * dtx + dtz * dtz) >= this.radius * this.radius); //while ((dtx * dtx + dtz * dtz) <= this.radius * this.radius) ;
                        if (i % 2 == 0)
                        {
                            //dtx = -dtx;
                            dtz = -dtz;
                        }
                        translationArr[i] = dtx.ToString() + " " + "0 " + dtz.ToString();
                    }
                }//end of random
                //else
                {
                    
                }//end of line
            }
            #endregion
            #region 月牙区域moon
            else if (this.profile.Equals("moon"))//月牙区域moon
            {
                //if (this.control.Equals("random"))//如果random,则随机生成
                {
                    long tick = DateTime.Now.Ticks;
                    Random rand = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
                    double dtx, dtz;

                    for (int i = 0; i < this.number; i++)
                    {
                        do
                        {
                            dtx = rand.NextDouble() * (this.radius - 0.1) + 0.1;//Math.random() * (width - 0.1) + 0.1;
                            dtz = rand.NextDouble() * (this.radius - 0.1) + 0.1; ;//Math.random() * (width - 0.1) + 0.1;
                        } while ((dtx * dtx + dtz * dtz) >= this.radius * this.radius || (dtx + 2) * (dtx + 2) + (dtz + 2) * (dtz + 2) <= (this.radius+1) * (this.radius+1)); //while ((dtx * dtx + dtz * dtz) <= this.radius * this.radius) ;
                        if (i % 2 == 0)
                        {
                            //dtx = -dtx;
                            dtz = -dtz;
                        }
                        translationArr[i] = dtx.ToString() + " " + "0 " + dtz.ToString();
                    }
                }//end of random
                //else
                {

                }//end of line
            }
            #endregion
            else//其他，返回方形
            {
                long tick = DateTime.Now.Ticks;
                Random rand = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
                double dtx, dtz;
                for (int i = 0; i < this.number; i++)
                {
                    dtx = rand.NextDouble() * (this.radius - 0.1) + 0.1;
                    dtz = rand.NextDouble() * (this.radius - 0.1) + 0.1; 
                    translationArr[i] = dtx.ToString() + " " + "0 " + dtz.ToString();
                }
            }
            return this.translationArr;
        }
    }
}
